<template>
  <div>
    <el-table :data="tableData" style="width: 100%" height="700">
      <!-- <el-table :data="tableData" style="width: 100%"> -->
      <el-table-column fixed type="selection" width="55"> </el-table-column>

      <el-table-column label="ID" width="100">
        <template slot-scope="scope">
          <!-- <i class="el-icon-time"></i> -->
          <span style="margin-left: 10px">{{ scope.$index+1 }}</span>
        </template>
      </el-table-column>

      <el-table-column label="用户名" width="150">
        <template slot-scope="scope">
          <!-- <i class="el-icon-time"></i> -->
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>

      <el-table-column label="昵称" width="100">
        <template slot-scope="scope">
          <!-- <i class="el-icon-time"></i> -->
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="120">
        <template slot-scope="scope">
          <!-- <i class="el-icon-time"></i> -->

          <el-tag v-if="scope.row.status == 1" >普通用户</el-tag>
          <el-tag type="success" v-else-if="scope.row.status == 2">管理员</el-tag>
          <el-tag type="warning" v-else-if="scope.row.status == 3">集群管理员</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status == 4">超级管理员</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="200">
        <template slot-scope="scope">
          <!-- <i class="el-icon-time"></i> -->
          <span>{{ scope.row.create_at }}</span>
        </template>
      </el-table-column>

      <el-table-column label="更新时间" width="200">
        <template slot-scope="scope">
          <!-- <i class="el-icon-time"></i> -->
          <span>{{ scope.row.update_at }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="Resetpsd(scope.row)"
            >重置密码</el-button>
          <el-button size="mini" @click="showDialog(scope.row['id'])"
            >权限修改</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  权限修改弹窗-->
    <el-dialog title="权限修改" :visible.sync="statusDialogVisible">
     <el-form>
      <el-form-item label="权限" :label-width="formLabelWidth">
        <el-select v-model="value" placeholder="请选择" @change="change()">
          <el-option label="超级管理员" value="4"></el-option>
          <el-option label="集群管理员" value="3"></el-option>
          <el-option label="管理员" value="2"></el-option>
          <el-option label="普通用户" value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="statusDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="Resetlev(value)">确 定</el-button>
    </div>
   </el-dialog>
  </div>
</template>

<script>
import Axios from "axios";
import qs from "qs";

var new_id;
export default {
  created() {
    this.getinfo();
  },
  data() {
    return {
      value: "",
      formLabelWidth: "120px",
      statusDialogVisible: false,
      rolelist: ["", "普通用户", "管理员", "集群管理员", "超级管理员"],
      colorlist: ["", "black", "green", "blue", "red"],
      tableData: [
        {
          id: "",
          username: "",
          nickname: "",
          status: "",
          create_at: "",
          update_at: "",
        },
      ],
    };
  },
  methods: {
    Resetpsd(row) {
      this.$prompt("请输入密码", {
        formtype: 1,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        // inputErrorMessage: '邮箱格式不正确'
      })
        .then(({ value }) => {
          var url = "/user/resetpass/" + row["id"];
          Axios.post(
            url,
            qs.stringify({
              password: value,
            })
          ).then((res) => {
            if (res.data.state == 1) {
              this.$message({
                type: "success",
                message: res.data.msg,
              });
            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    change(){
      this.$forceUpdate()
    },
    showDialog(id){
      // console.log("1111111111111111111111111")
      this.statusDialogVisible = true;
      new_id = id;
    },
    Resetlev(value) {
      // console.log("2222222222222222")
          var url = "/user/update/" + new_id;
          Axios.post(
            url,
            qs.stringify({
              status: value,
            })
          ).then((res) => {
            if (res.data.state == 1) {
              this.$message({
                type: "success",
                message: res.data.msg,
              });
            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
              });
            }
          })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
      this.statusDialogVisible = false;
      location.reload();
    },
    handleDelete(row) {
      /*提示消息*/
       this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          Axios.get("/user/delete/" + row['id']).then((res) => {
              this.$message({
              type: 'success',
              message: res.data.msg
            });
              location.reload();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    getinfo: function () {
      Axios.get("/user").then((res) => {
        this.tableData = res.data.msg;
        console.log(res.data.msg);
      });
    },
  },
};
</script>